<?php

require '../vendor/autoload.php';

use Elasticsearch\ClientBuilder;

class Es
{
    /**
     * 类变量
     * @var \Elasticsearch\Client
     */
    private $es;

    /**
     * Es constructor.
     * @param array $hosts
     */
    public function __construct($hosts = ['127.0.0.1:9200'])
    {
        $this->es = ClientBuilder::create()->setHosts($hosts)->build();
    }

    /**
     * 获取单个文档
     * @param $index
     * @param $id
     */
    public function getDoc($index, $id)
    {
        $params = [
            'index' => $index,
            'id'    => $id
        ];
        return $this->es->get($params);
    }

    /**
     * 批量获取文档
     * @param $index
     * @param array $ids
     */
    public function getDocs($index, array $ids)
    {
        $params = [
            'index' => $index,
            'body'  => [
                'ids' => $ids
            ],
        ];
        return $this->es->mget($params);
    }

    /**
     * 更新单个文档
     * @param $index
     * @param $id
     * @param $data
     */
    public function updateDoc($index, $id, $data)
    {
        $params = [
            'index' => $index,
            'id'    => $id,
            'body'  => [
                'doc' => $data
            ],
        ];
        return $this->es->update($params);
    }




}